package com.dao;

import java.util.List;

import com.entity.Function;
import com.util.Page;

@SuppressWarnings("unchecked")
public class FunctionDao extends BaseDAO {

	/**
	 * 保存
	 * @param emp
	 * @return
	 */
	public boolean save(Function function){
		return this.saveObject(function);
	}
	
	/**
	 * 更新EMPloyee
	 * @param emp
	 * @return
	 */
	public boolean updateFunction(Function function){
		return this.updateObject(function, "where :id=?",function.getId());
	}
	/**
	 * 删除
	 * @param function
	 * @return
	 */
	public boolean del(String ids){
		return this.executeUpdate("delete from function where id in("+ids+")");
	}
	
	/**
	 * 根据ID查对象
	 * @param id
	 * @return Function
	 * @throws Exception
	 */
	public Function fundById(int id) throws Exception{
		List<Function> list = this.queryObject(Function.class,"where :id=?",id);
		Function function = null;
		if(list != null && !list.isEmpty()){
			function = list.get(0);
		}
		return function;
	}
	
	/**
	 * 检查名称是否存在
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public boolean checkName(String name) throws Exception{
		List list = this.queryObject(Function.class,"where functionname=?",name);
		return list.isEmpty();
	}
	
	/**
	 * 取list
	 * @return
	 */
	public List findBylist(){
		List list = null;
		try {
			list =  this.queryObject(Function.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 取分页数据
	 * @param page
	 * @return Page
	 * @throws Exception
	 */
	public Page findbyPage(Page page) throws Exception{
		return this.queryObjectForPage(Function.class, page);
	}
	
	/**
	 * 取用户角色功能列表
	 * @param roleid
	 * @return
	 */
	public List findFunctionsByRole(int roleid){
		String sql = "select A.id,A.functionname,parentid from function A,roles B,role_functions C where A.id=C.functionid and B.id=C.roleid and B.id="+roleid;
		return findObjectBySql(Function.class, sql);
	}
	
	/**
	 * 查询一级菜单
	 * @return
	 * @throws Exception
	 */
	public List findTreeTopNode() throws Exception{
		return this.findObjectBySql(Function.class,"select * from function where parentid=0");
	}
	
	
	/**
	 * 查询所有功能list
	 * @return
	 * @throws Exception
	 */
	public List findFunctionList() throws Exception{
//		return this.findObjectBySql(Function.class,"select * from function where parentid=0");
		return this.queryObject(Function.class);
	}
	
	/**
	 * 根据ID取子菜单
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public List findTreeNodeById(int id) throws Exception{
		return this.queryObject(Function.class,"where :parentid=?",id);
	}
}
